<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>flowlayout.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-graphicsview-flowlayout-example.html">Graphics View Flow Layout Example</a></td><td >flowlayout.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">flowlayout.cpp 示例文件</h1>
<span class="subtitle">graphicsview/flowlayout/flowlayout.cpp</span>
<!-- $$$graphicsview/flowlayout/flowlayout.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &quot;flowlayout.h&quot;</span>

  <span class="preprocessor">#include &lt;qmath.h&gt;</span>

  <span class="preprocessor">#include &lt;QWidget&gt;</span>

  FlowLayout<span class="operator">::</span>FlowLayout()
  {
      m_spacing<span class="operator">[</span><span class="number">0</span><span class="operator">]</span> <span class="operator">=</span> <span class="number">6</span>;
      m_spacing<span class="operator">[</span><span class="number">1</span><span class="operator">]</span> <span class="operator">=</span> <span class="number">6</span>;
      <span class="type"><a href="qsizepolicy.html">QSizePolicy</a></span> sp <span class="operator">=</span> sizePolicy();
      sp<span class="operator">.</span>setHeightForWidth(<span class="keyword">true</span>);
      setSizePolicy(sp);
  }

  <span class="type">void</span> FlowLayout<span class="operator">::</span>insertItem(<span class="type">int</span> index<span class="operator">,</span> <span class="type"><a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a></span> <span class="operator">*</span>item)
  {
      item<span class="operator">-</span><span class="operator">&gt;</span>setParentLayoutItem(<span class="keyword">this</span>);
      <span class="keyword">if</span> (<span class="type"><a href="../qtcore/qtglobal.html#uint-typedef">uint</a></span>(index) <span class="operator">&gt;</span> <span class="type"><a href="../qtcore/qtglobal.html#uint-typedef">uint</a></span>(m_items<span class="operator">.</span>count()))
          index <span class="operator">=</span> m_items<span class="operator">.</span>count();
      m_items<span class="operator">.</span>insert(index<span class="operator">,</span> item);
      invalidate();
  }

  <span class="type">int</span> FlowLayout<span class="operator">::</span>count() <span class="keyword">const</span>
  {
      <span class="keyword">return</span> m_items<span class="operator">.</span>count();
  }

  <span class="type"><a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a></span> <span class="operator">*</span>FlowLayout<span class="operator">::</span>itemAt(<span class="type">int</span> index) <span class="keyword">const</span>
  {
      <span class="keyword">return</span> m_items<span class="operator">.</span>value(index);
  }

  <span class="type">void</span> FlowLayout<span class="operator">::</span>removeAt(<span class="type">int</span> index)
  {
      m_items<span class="operator">.</span>removeAt(index);
      invalidate();
  }

  <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> FlowLayout<span class="operator">::</span>spacing(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Orientation o) <span class="keyword">const</span>
  {
      <span class="keyword">return</span> m_spacing<span class="operator">[</span><span class="type">int</span>(o) <span class="operator">-</span> <span class="number">1</span><span class="operator">]</span>;
  }

  <span class="type">void</span> FlowLayout<span class="operator">::</span>setSpacing(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Orientations o<span class="operator">,</span> <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> spacing)
  {
      <span class="keyword">if</span> (o <span class="operator">&amp;</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal)
          m_spacing<span class="operator">[</span><span class="number">0</span><span class="operator">]</span> <span class="operator">=</span> spacing;
      <span class="keyword">if</span> (o <span class="operator">&amp;</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Vertical)
          m_spacing<span class="operator">[</span><span class="number">1</span><span class="operator">]</span> <span class="operator">=</span> spacing;
  }

  <span class="type">void</span> FlowLayout<span class="operator">::</span>setGeometry(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span> <span class="operator">&amp;</span>geom)
  {
      <span class="type"><a href="qgraphicslayout.html">QGraphicsLayout</a></span><span class="operator">::</span>setGeometry(geom);
      doLayout(geom<span class="operator">,</span> <span class="keyword">true</span>);
  }

  <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> FlowLayout<span class="operator">::</span>doLayout(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span> <span class="operator">&amp;</span>geom<span class="operator">,</span> bool applyNewGeometry) <span class="keyword">const</span>
  {
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> left<span class="operator">,</span> top<span class="operator">,</span> right<span class="operator">,</span> bottom;
      getContentsMargins(<span class="operator">&amp;</span>left<span class="operator">,</span> <span class="operator">&amp;</span>top<span class="operator">,</span> <span class="operator">&amp;</span>right<span class="operator">,</span> <span class="operator">&amp;</span>bottom);
      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> maxw <span class="operator">=</span> geom<span class="operator">.</span>width() <span class="operator">-</span> left <span class="operator">-</span> right;

      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> x <span class="operator">=</span> <span class="number">0</span>;
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> y <span class="operator">=</span> <span class="number">0</span>;
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> maxRowHeight <span class="operator">=</span> <span class="number">0</span>;
      <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> pref;
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> m_items<span class="operator">.</span>count(); <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="type"><a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a></span> <span class="operator">*</span>item <span class="operator">=</span> m_items<span class="operator">.</span>at(i);
          pref <span class="operator">=</span> item<span class="operator">-</span><span class="operator">&gt;</span>effectiveSizeHint(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>PreferredSize);
          maxRowHeight <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(maxRowHeight<span class="operator">,</span> pref<span class="operator">.</span>height());

          <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> next_x;
          next_x <span class="operator">=</span> x <span class="operator">+</span> pref<span class="operator">.</span>width();
          <span class="keyword">if</span> (next_x <span class="operator">&gt;</span> maxw) {
              <span class="keyword">if</span> (x <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) {
                  pref<span class="operator">.</span>setWidth(maxw);
              } <span class="keyword">else</span> {
                  x <span class="operator">=</span> <span class="number">0</span>;
                  next_x <span class="operator">=</span> pref<span class="operator">.</span>width();
              }
              y <span class="operator">+</span><span class="operator">=</span> maxRowHeight <span class="operator">+</span> spacing(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Vertical);
              maxRowHeight <span class="operator">=</span> <span class="number">0</span>;
          }

          <span class="keyword">if</span> (applyNewGeometry)
              item<span class="operator">-</span><span class="operator">&gt;</span>setGeometry(<span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span>(<span class="type"><a href="../qtcore/qpointf.html">QPointF</a></span>(left <span class="operator">+</span> x<span class="operator">,</span> top <span class="operator">+</span> y)<span class="operator">,</span> pref));
          x <span class="operator">=</span> next_x <span class="operator">+</span> spacing(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal);
      }
      maxRowHeight <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(maxRowHeight<span class="operator">,</span> pref<span class="operator">.</span>height());
      <span class="keyword">return</span> top <span class="operator">+</span> y <span class="operator">+</span> maxRowHeight <span class="operator">+</span> bottom;
  }

  <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> FlowLayout<span class="operator">::</span>minSize(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> <span class="operator">&amp;</span>constraint) <span class="keyword">const</span>
  {
      <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> size(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> left<span class="operator">,</span> top<span class="operator">,</span> right<span class="operator">,</span> bottom;
      getContentsMargins(<span class="operator">&amp;</span>left<span class="operator">,</span> <span class="operator">&amp;</span>top<span class="operator">,</span> <span class="operator">&amp;</span>right<span class="operator">,</span> <span class="operator">&amp;</span>bottom);
      <span class="keyword">if</span> (constraint<span class="operator">.</span>width() <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span>) {   <span class="comment">// height for width</span>
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> height <span class="operator">=</span> doLayout(<span class="type"><a href="../qtcore/qrectf.html">QRectF</a></span>(<span class="type"><a href="../qtcore/qpointf.html">QPointF</a></span>(<span class="number">0</span><span class="operator">,</span><span class="number">0</span>)<span class="operator">,</span> constraint)<span class="operator">,</span> <span class="keyword">false</span>);
          size <span class="operator">=</span> <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span>(constraint<span class="operator">.</span>width()<span class="operator">,</span> height);
      } <span class="keyword">else</span> <span class="keyword">if</span> (constraint<span class="operator">.</span>height() <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span>) {  <span class="comment">// width for height?</span>
          <span class="comment">// not supported</span>
      } <span class="keyword">else</span> {
          <span class="type"><a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a></span> <span class="operator">*</span>item;
          foreach (item<span class="operator">,</span> m_items)
              size <span class="operator">=</span> size<span class="operator">.</span>expandedTo(item<span class="operator">-</span><span class="operator">&gt;</span>effectiveSizeHint(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>MinimumSize));
          size <span class="operator">+</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(left <span class="operator">+</span> right<span class="operator">,</span> top <span class="operator">+</span> bottom);
      }
      <span class="keyword">return</span> size;
  }

  <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> FlowLayout<span class="operator">::</span>prefSize() <span class="keyword">const</span>
  {
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> left<span class="operator">,</span> right;
      getContentsMargins(<span class="operator">&amp;</span>left<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">&amp;</span>right<span class="operator">,</span> <span class="number">0</span>);

      <span class="type"><a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a></span> <span class="operator">*</span>item;
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> maxh <span class="operator">=</span> <span class="number">0</span>;
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> totalWidth <span class="operator">=</span> <span class="number">0</span>;
      foreach (item<span class="operator">,</span> m_items) {
          <span class="keyword">if</span> (totalWidth <span class="operator">&gt;</span> <span class="number">0</span>)
              totalWidth <span class="operator">+</span><span class="operator">=</span> spacing(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal);
          <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> pref <span class="operator">=</span> item<span class="operator">-</span><span class="operator">&gt;</span>effectiveSizeHint(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>PreferredSize);
          totalWidth <span class="operator">+</span><span class="operator">=</span> pref<span class="operator">.</span>width();
          maxh <span class="operator">=</span> <a href="../qtcore/qtglobal.html#qMax">qMax</a>(maxh<span class="operator">,</span> pref<span class="operator">.</span>height());
      }
      maxh <span class="operator">+</span><span class="operator">=</span> spacing(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Vertical);

      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> goldenAspectRatio <span class="operator">=</span> <span class="number">1.61803399</span>;
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> w <span class="operator">=</span> <a href="../qtcore/qtmath.html#qSqrt">qSqrt</a>(totalWidth <span class="operator">*</span> maxh <span class="operator">*</span> goldenAspectRatio) <span class="operator">+</span> left <span class="operator">+</span> right;

      <span class="keyword">return</span> minSize(<span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span>(w<span class="operator">,</span> <span class="operator">-</span><span class="number">1</span>));
  }

  <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> FlowLayout<span class="operator">::</span>maxSize() <span class="keyword">const</span>
  {
      <span class="type"><a href="qgraphicslayoutitem.html">QGraphicsLayoutItem</a></span> <span class="operator">*</span>item;
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> totalWidth <span class="operator">=</span> <span class="number">0</span>;
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> totalHeight <span class="operator">=</span> <span class="number">0</span>;
      foreach (item<span class="operator">,</span> m_items) {
          <span class="keyword">if</span> (totalWidth <span class="operator">&gt;</span> <span class="number">0</span>)
              totalWidth <span class="operator">+</span><span class="operator">=</span> spacing(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Horizontal);
          <span class="keyword">if</span> (totalHeight <span class="operator">&gt;</span> <span class="number">0</span>)
              totalHeight <span class="operator">+</span><span class="operator">=</span> spacing(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Vertical);
          <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> pref <span class="operator">=</span> item<span class="operator">-</span><span class="operator">&gt;</span>effectiveSizeHint(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>PreferredSize);
          totalWidth <span class="operator">+</span><span class="operator">=</span> pref<span class="operator">.</span>width();
          totalHeight <span class="operator">+</span><span class="operator">=</span> pref<span class="operator">.</span>height();
      }

      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> left<span class="operator">,</span> top<span class="operator">,</span> right<span class="operator">,</span> bottom;
      getContentsMargins(<span class="operator">&amp;</span>left<span class="operator">,</span> <span class="operator">&amp;</span>top<span class="operator">,</span> <span class="operator">&amp;</span>right<span class="operator">,</span> <span class="operator">&amp;</span>bottom);
      <span class="keyword">return</span> <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span>(left <span class="operator">+</span> totalWidth <span class="operator">+</span> right<span class="operator">,</span> top <span class="operator">+</span> totalHeight <span class="operator">+</span> bottom);
  }

  <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> FlowLayout<span class="operator">::</span>sizeHint(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>SizeHint which<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> <span class="operator">&amp;</span>constraint) <span class="keyword">const</span>
  {
      <span class="type"><a href="../qtcore/qsizef.html">QSizeF</a></span> sh <span class="operator">=</span> constraint;
      <span class="keyword">switch</span> (which) {
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>PreferredSize:
          sh <span class="operator">=</span> prefSize();
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>MinimumSize:
          sh <span class="operator">=</span> minSize(constraint);
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>MaximumSize:
          sh <span class="operator">=</span> maxSize();
          <span class="keyword">break</span>;
      <span class="keyword">default</span>:
          <span class="keyword">break</span>;
      }
      <span class="keyword">return</span> sh;
  }

</pre>
</div>
<!-- @@@graphicsview/flowlayout/flowlayout.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
